<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <script>
        function ajax(option) {
            // console.log(option)
            var xhr = new XMLHttpRequest();
            var str = '';
            for (k in option.data) {
                str += k + '=' + option.data[k] + '&'
            }
            if (str.length > 0 && str.lastIndexOf('&') === str.length - 1) {
                str.substr(0, str.length - 1);
            }
            if (option.type === 'get') {
                var strUrl = option.url + '?' + encodeURI(str);
            } else if (option.type === 'post') {
                var strUrl = option.url;
            }

            xhr.open(option.type, strUrl)
            if (option.type === 'get') {
                xhr.send(null)
            } else if (option.type === 'post') {
                xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
                xhr.send(str)
            }

            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    var res = xhr.responseText;
                    var obj = JSON.parse(res)
                    option.success(obj)
                }
            }
        }
        ajax({
            url: 'http://www.liulongbin.top:3006/api/addbook',

            type: 'post',
            data: {
                bookname: '111111',
                author: '2222222222',
                publisher: '33333333'
            },
            success: function(res) {
                console.log(res)
            }

        })
    </script>
</body>

</html>